$(function () {


    listClassesTaskReply(1);

    $('body')
        .on('click', '#refresh', function() {
            $('input[name=keyword]').val('');
            listClassesTaskReply(1);
        })
        .on('click', '#btn1, #btn2', function() {
            listClassesTaskReply(1);
        })
        .on('click', 'i.fa-trash', function () { // 单个删除
            var ids = $(this).parent('a').data('id');
            var  that =$(this).parent('a');
            icModale(2, '确认删除选中的回复吗?', '', function () {
                updateReplyStatus(ids, 2,that);
            })
        }).on("click",".feed-photo",function(){
           var  href = $(this).attr('src');
            parent.layer.open({
                type: 1,
                area: winArea,
                shadeClose: true,
                skin: 'layui-layer-demo',
                title: false,
                content: '<div class="text-center"><img  src='+href+'></div>',
                btn: false
            });
        }).on("click",".allReply",function(){
            $(this).addClass("reply").removeClass("allReply").text("收起");
                getAllReply($(this).data("id"),true);
        }).on("click",".reply",function(){
        $(this).addClass("allReply").removeClass("reply").text("查看全部");
             getAllReply($(this).data("id"),false);
    });

    function updateReplyStatus(ids, status,that) {
        if (!ids) {
            icModale(0, '请选择要操作的话题评论');
            return;
        }
        $.ajax({
            url: baseloaction + '/admin/classes/topic/reply/updateReplyStatus',
            data: {'ids': ids, 'status': status},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                icModale(0, response.message);
                if (response.success) {
                   $("#content_"+ids).html("该条内容已被管理员删除");
                    that.remove();
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        });
    }


    function getAllReply(id,setAllLeaft) {
        $.ajax({
            url: baseloaction + '/admin/classes/topic/reply/allReply',
            data: {'id': id,"setAllLeaft":setAllLeaft},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                var left =$(".reply_"+id);
                $.each(left,function (index,date) {
                    $(date).remove();
                });
              var leafList =  response.list[0].leafList;
              if(leafList.length>0){
                var html ="";

                var content =   response.list;
                if(content.length>0){
                var leafList   =   content[0].leafList;
                    $.each(leafList,function (index,date) {

                        html +=  '    <div class="media reply_'+id+'">' +
                            '                        <div class="media-left">' +
                            '                            <a href="#">' +
                            '                                <img class="media-object lazy"   style="width: 34px; height: 34px;" data-original="'+date.user.avatar+'" data-holder-rendered="true">' +
                            '                            </a>' +
                            '                        </div>' +
                            '                        <div class="media-body">';
                            if(date.status==1){
                                html+=    ' <aside class="pull-right m-r-lg" >' +
                                ' <a href="javascript:void(0)" class="c-master fs14 ml5" data-toggle="tooltip" data-id="'+date.id+'" data-placement="top" data-original-title="删除"><i class="fa fa-trash"></i> </a>' +
                                '   </aside>';
                            }

                        html+=    '                            <span class="c-master">'+date.user.nickname+'</span>';

                              if(date.targetUser!=null&&date.targetId!=date.rootId){
                                html+=    '<span class="c-master">@</span>' +
                                '           <span class="c-master">'+date.targetUser.nickname+'</span>';
                             }
                         html+= '                            <small class="fs12 ml10 c-999">'+date.createTime.substring(0,16)+'</small>' +
                            '                            <div>' +
                            '                                <p id="content_'+date.id+'">' + date.content+'</p>' +
                            '                            </div>' +
                            '                        </div>' +
                            '                    </div>';

                     });
                    $("#reply_"+id).append(html);
                    layImg();
                }




              }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        });
    }

});

/**
 * 加载话题回复数据
 * @param pageNo
 */
function listClassesTaskReply(pageNo) {
    var params = $('#search-form').serializeJSON();
    params.topicId = $('#id').val();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/classes/topic/reply/ajaxTopicReplyListPage',//请求URL
        contextId: 'oc-ibox-content',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: listClassesTaskReply, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'all-select',        //全选框ID
        callback: function () {
            $('#count').html(total);
        }
    };
    ajaxGetListData(obj);
    layImg();
}

function layImg(){
    $('img.lazy').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-face.gif',  //占位图
        effect: 'fadeIn' ,//载入方式
        threshold: 200, // 提前开始加载
    });
}